package com.yj.lottery_system.service.activityStatus.operator;

import com.yj.lottery_system.dao.dataObject.ActivityPrizeDO;
import com.yj.lottery_system.dao.mapper.ActivityPrizeMapper;
import com.yj.lottery_system.service.dto.ConvertActivityStatusDTO;
import com.yj.lottery_system.service.enums.ActivityPrizeStatusEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator{
    @Resource
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //活动id
        Long activityId = convertActivityStatusDTO.getActivityId();
        //奖品id
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        //目标状态
        ActivityPrizeStatusEnum targetActivityPrizeStatus = convertActivityStatusDTO.getTargetActivityPrizeStatus();
        if(null == activityId
                || null == prizeId
                || null == targetActivityPrizeStatus ) {
            return false;
        }
        //拿奖品
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectByAPId(activityId, prizeId);

        if(null == activityPrizeDO) {
            return false;
        }
        //当前奖品状态与目标状态
        if(activityPrizeDO.getStatus().equalsIgnoreCase(targetActivityPrizeStatus.name())) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityPrizeMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getPrizeId(),
                    convertActivityStatusDTO.getTargetActivityPrizeStatus().name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }
}
